package com.watering.plan;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;

public class ChangePlant extends Activity {
	String plantName;
	String plantType;
	String plant;
	AutoCompleteTextView textView;
	EditText ed;
	SeekBar sb1;
	SeekBar sb2;
	SeekBar sb3;
	Toast ts;
	Base bs;
	
	public ArrayList<String> getPlants(){
		ArrayList<String> plant = new ArrayList();
	    Base bs = new Base(this);
	    try{
				bs.createDataBase();
				bs.openDataBase();
				plant = bs.getPlantArray();
				bs.close();
		}catch(Exception e){
		}
		
		return plant;
		
	}
	
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
         setContentView(R.layout.changeplant);
    //     Button myb = (Button) findViewById(R.id.button1);
     	String value = new String();
    	Bundle extras = getIntent().getExtras(); 
    	if(extras !=null)
    		 value = extras.getString("plantname");
         
         ts =  Toast.makeText(this, R.string.new_plant_help, Toast.LENGTH_SHORT);
         ts.setDuration(Toast.LENGTH_LONG);
         
         ImageView img = (ImageView)findViewById(R.id.helpbutton);
         img.setOnClickListener(new OnClickListener() {
	         public void onClick(View v) {
	        	ts.show();
	         }
	     });
         
         ArrayList<String> PLANTS = getPlants();
         
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, PLANTS);
         textView = (AutoCompleteTextView)
                 findViewById(R.id.autocomplete);
         textView.setAdapter(adapter);
         
         
         ed = new EditText(this);
         ed = (EditText)findViewById(R.id.plantname);
         ed.setText(value);
         
         sb1 = new SeekBar(this);
         sb1.setMax(10);
         sb1 = (SeekBar)findViewById(R.id.SeekBar01);
         
         sb2 = new SeekBar(this);
         sb2.setMax(10);
         sb2 = (SeekBar)findViewById(R.id.SeekBar02);
         
         sb3 = new SeekBar(this);
         sb3.setMax(10);
         sb3 = (SeekBar)findViewById(R.id.SeekBar03);
         
         bs = new Base(this);
	     
	     Button SaveButton = (Button) findViewById(R.id.buttonSave);
	     SaveButton.setOnClickListener(new OnClickListener() {
	         public void onClick(View v) {
	        	String plantName = ed.getText().toString();
	        	String plantType = textView.getText().toString();
	        	int pot = sb1.getProgress();
	        	int light = sb2.getProgress();
	        	int fol = sb3.getProgress();
	    	   
	    	    try{
	    				bs.createDataBase();
	    				bs.openDataBase();
	    				bs.insertRegistered(plantName,plantType,pot,fol,light);
	    				bs.close();
	    				
	    		}catch(Exception e){
	    			ed.setText("buba" + e);
	    		}
	    		finish();
	         }
	     });
	}  
}